<?php

class Provinces extends MX_Controller
{
    function __construct() {
        parent::__construct();
         $this->load->library('pagination');  
    }
    
    /**
     * 
     * @param type $row
     */
    function index($row=0)
    {
        if(!($this->session->userdata('login'))) redirect('administrator/login');
            //Check Quyền
            //if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.index'))))
            //redirect('administrator/index');
                        
            if($this->input->post('txtname'))
            {
                //add Term
                $province = new Province();
                $province->name = $this->input->post('txtname');
                $province->code = $this->input->post('txtcode');
                
                $province->save();
            }
            
            $province = new Province();            
            
            include('paging.php');
            $config['base_url']= base_url()."administrator/provinces/index/";
            $config['total_rows'] = $province->count();
            $config['cur_page']= $row;
            $config['num_links'] = 3;		
            $this->pagination->initialize($config);
            $data['list_link'] = $this->pagination->create_links();	
                      
            $data['lstProvince'] = $province->limit($config['per_page'], $row)->get();
            $data['view'] = 'provinces_index';
            $this->load->view('back_end/template_noright',$data);
    }
    
    /**
     * 
     * @param type $id
     * @param type $row
     */
    function edit($id,$row=0)
    {
        if(!($this->session->userdata('login'))) redirect('administrator/login');
        //Check Quyền
        //if(!($this->session->userdata('login')&& ($this->User_identity->check_acess('category.index'))))
        //redirect('administrator/index');

        if($this->input->post('txtname'))
        {
            $id= $this->input->post('province_id');
            //add Term
            $province = new Province();
            $province->where('id',$id)->get();
            $province->name = $this->input->post('txtname');
            $province->code = $this->input->post('txtcode');

            $province->save();
            redirect('administrator/provinces');
        }

        $province = new Province();        
        
        include('paging.php');
        $config['base_url']= base_url()."administrator/provinces/edit/".$id.'/';
        $config['total_rows'] = $province->count();
        $config['cur_page']= $row;
        $config['num_links'] = 3;		
        $this->pagination->initialize($config);
        $data['list_link'] = $this->pagination->create_links();	

        $data['lstProvince'] = $province->limit($config['per_page'], $row)->get();
        
        $province = new Province();
        $data['province'] = $province->where('id',$id)->get();
        $data['view'] = 'provinces_edit';
        $this->load->view('back_end/template_noright',$data);
    }
    
    function delete()
    {
        $id = $this->input->post('param');
        $province = new Province();
        $province->where('id',$id)->get();
        $province->delete();
    }
}
?>
